#!/usr/bin/python3

# https://blog.csdn.net/weixin_44244190/article/details/129260542

from dvg_ringbuffer import RingBuffer
import threading
import numpy as np


class GlobVarLocker():
    def init(originDataRBSize):
        '''
        [1] ringbuffer store that origin Data from serial port
        '''
        global originDataThrdLock
        originDataThrdLock = threading.Lock()

        global originDataRB
        originDataRB = RingBuffer(originDataRBSize, dtype=int)

        '''
        [2] ringbuffer store gyro[3]
        '''
        global gyroDataThrdLock
        gyroDataThrdLock = threading.Lock()

        global gyroDataRB
        gyroDataRB = RingBuffer(200, dtype=(float, 3))
        '''
        [3] ringbuffer store quaternion[4]
        '''
        global quatDataThrdLock
        quatDataThrdLock = threading.Lock()

        global quatDataRB
        quatDataRB = RingBuffer(200, dtype=(float, 4))

        '''
        [4] ringbuffer store acc[3]
        '''
        global accDataThrdLock
        accDataThrdLock = threading.Lock()

        global accDataRB
        accDataRB = RingBuffer(200, dtype=(float, 3))

        '''
        [5] ringbuffer store pose[3]
        '''
        global poseDataThrdLock
        poseDataThrdLock = threading.Lock()

        global poseDataRB
        poseDataRB = RingBuffer(200, dtype=(float, 3))

        '''
        [6] ringbuffer store Magnetometer[3]
        '''
        global magnDataThrdLock
        magnDataThrdLock = threading.Lock()

        global magnDataRB
        magnDataRB = RingBuffer(200, dtype=(np.int16, 3))

        '''
        [7] ringbuffer store temperature, altitude, press
        '''
        global envDataThrdLock
        envDataThrdLock = threading.Lock()

        global envDataRB
        envDataRB = RingBuffer(200, dtype=(float, 3))


    '''
    [1] ringbuffer store that origin Data from serial port
    '''
    def lockOriginDataRB():
        originDataThrdLock.acquire()

    def unlockOriginDataRB():
        originDataThrdLock.release()


    def printOriginDataRB():
        # TODO modify
        if (len(originDataRB) > 0):
            print((originDataRB.popleft()), end=' ')

    def putOriginDataRB(newDataFlow):
        originDataRB.extend(newDataFlow)

    def getOriginDataRBLen():
        return len(originDataRB)

    def getOriginDataRBMaxSize():
        return originDataRB.maxlen

    def popOriginDataRB():
        return originDataRB.popleft()



    '''
    [2] ringbuffer store gyro[3]
    '''
    def lockGyroDataRB():
        gyroDataThrdLock.acquire()

    def unlockGyroDataRB():
        gyroDataThrdLock.release()

    def printGyroDataRB():
        # TODO modify
        if (len(gyroDataRB) > 0):
            print("gyro", end=' ')
            print((gyroDataRB.popleft()))

    def putGyroDataRB(newDataFlow):
        gyroDataRB.append(newDataFlow)

    def getGyroDataRBLen():
        return len(gyroDataRB)

    def getGyroDataRBMaxSize():
        return gyroDataRB.maxlen

    def popGyroDataRB():
        return gyroDataRB.popleft()



    '''
    [3] ringbuffer store quaternion[4]
    '''

    def lockQuatDataRB():
        quatDataThrdLock.acquire()

    def unlockQuatDataRB():
        quatDataThrdLock.release()

    def printQuatDataRB():
        # TODO modify
        if (len(quatDataRB) > 0):
            print("quat", end=' ')
            print((quatDataRB.popleft()))

    def putQuatDataRB(newDataFlow):
        quatDataRB.append(newDataFlow)

    def getQuatDataRBLen():
        return len(quatDataRB)

    def getQuatDataRBMaxSize():
        return quatDataRB.maxlen

    def popQuatDataRB():
        return quatDataRB.popleft()


    '''
    [4] ringbuffer store acc[3]
    '''
    def lockAccDataRB():
        accDataThrdLock.acquire()

    def unlockAccDataRB():
        accDataThrdLock.release()

    def printAccDataRB():
        # TODO modify
        if (len(accDataRB) > 0):
            print("acc", end=' ')
            print((accDataRB.popleft()))

    def putAccDataRB(newDataFlow):
        accDataRB.append(newDataFlow)

    def getAccDataRBLen():
        return len(accDataRB)

    def getAccDataRBMaxSize():
        return accDataRB.maxlen

    def popAccDataRB():
        return accDataRB.popleft()



    '''
    [5] ringbuffer store pose[3]
    '''
    def lockPoseDataRB():
        poseDataThrdLock.acquire()

    def unlockPoseDataRB():
        poseDataThrdLock.release()

    def printPoseDataRB():
        # TODO modify
        if (len(poseDataRB) > 0):
            print("pose", end=' ')
            print((poseDataRB.popleft()))

    def putPoseDataRB(newDataFlow):
        poseDataRB.append(newDataFlow)

    def getPoseDataRBLen():
        return len(poseDataRB)

    def getPoseDataRBMaxSize():
        return poseDataRB.maxlen

    def popPoseDataRB():
        return poseDataRB.popleft()


    '''
    [6] ringbuffer store Magnetometer[3]
    '''
    def lockMagnDataRB():
        magnDataThrdLock.acquire()

    def unlockMagnDataRB():
        magnDataThrdLock.release()

    def printMagnDataRB():
        # TODO modify
        if (len(magnDataRB) > 0):
            print("magn", end=' ')
            print((magnDataRB.popleft()))

    def putMagnDataRB(newDataFlow):
        magnDataRB.append(newDataFlow)

    def getMagnDataRBLen():
        return len(magnDataRB)

    def getMagnDataRBMaxSize():
        return magnDataRB.maxlen

    def popMagnDataRB():
        return magnDataRB.popleft()

    '''
    [7] ringbuffer store temperature, altitude, press
    '''
    def lockEnvDataRB():
        envDataThrdLock.acquire()

    def unlockEnvDataRB():
        envDataThrdLock.release()

    def printEnvDataRB():
        # TODO modify
        if (len(envDataRB) > 0):
            print("env", end=' ')
            print((envDataRB.popleft()))

    def putEnvDataRB(newDataFlow):
        envDataRB.append(newDataFlow)

    def getEnvDataRBLen():
        return len(envDataRB)

    def getEnvDataRBMaxSize():
        return envDataRB.maxlen

    def popEnvDataRB():
        return envDataRB.popleft()